package com.bp;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Andhini
 */
public class Biro {

    public String id_biro;
    public String nama_biro;
    public String Alamat;
    public String no_telp;
    public String email_biro;

    public Biro() {
    }

    /**
     * @return the id_biro
     */
    public String getId_biro() {
        return id_biro;
    }

    /**
     * @param id_biro the id_biro to set
     */
    public void setId_biro(String id_biro) {
        this.id_biro = id_biro;
    }

    /**
     * @return the nama_biro
     */
    public String getNama_biro() {
        return nama_biro;
    }

    /**
     * @param nama_biro the nama_biro to set
     */
    public void setNama_biro(String nama_biro) {
        this.nama_biro = nama_biro;
    }

    /**
     * @return the Alamat
     */
    public String getAlamat() {
        return Alamat;
    }

    /**
     * @param Alamat the Alamat to set
     */
    public void setAlamat(String Alamat) {
        this.Alamat = Alamat;
    }

    /**
     * @return the no_telp
     */
    public String getNo_telp() {
        return no_telp;
    }

    /**
     * @param no_telp the no_telp to set
     */
    public void setNo_telp(String no_telp) {
        this.no_telp = no_telp;
    }

    /**
     * @return the email_biro
     */
    public String getEmail_biro() {
        return email_biro;
    }

    /**
     * @param email_biro the email_biro to set
     */
    public void setEmail_biro(String email_biro) {
        this.email_biro = email_biro;
    }

    public void insertDataBiro() {
        try {
            Connection connection = new DbBean_1().getKoneksi();
            Statement s = connection.createStatement();
//            String sql = "insert into PAKET values('" + idPaket + "', '" + namaPaket + "', to_Date('" + waktu + "', 'DD-MM-YY'), " + harga + ",'" + armada + "','" + fasilitas + "','" + tujuan + "'," + kuota + ")";
            String sql = "insert into Biro values('" + id_biro + "', '" + nama_biro + "','" + Alamat + "','" + no_telp + "','" + email_biro + "')";
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Selamat Anda sudah terdaftar menjadi Biro");
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Maaf Anda gagal melakukan pendaftaran,silahkan lakukan isi data kembali/n" + ex);
            Logger.getLogger(Biro.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public final String getId_Biro() {
 try {
            String sql = "select max(ID_BIRO) from BIRO";
            Connection con = new DbBean_1().getKoneksi();
            java.sql.Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(sql);

            rs.next();
            String result = rs.getString(1);
            if (result != null) {
                int no = Integer.parseInt(result);
                no++;
                sql = "" + no;
                while (sql.length() <= 3) {
                    sql = "0" + sql;
                }
            } else {
                sql = "0001";
            }
            return sql;
        } catch (SQLException ex) {
            Logger.getLogger(Biro.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Boolean updateBiro(String BiroId) {
        int result = 0;
        try {
            Connection conn = new DbBean_1().getKoneksi();
            Statement s = conn.createStatement();

            String sql = "UPDATE BIRO SET NAMA_BIRO='" + nama_biro + "',ALAMAT='" + Alamat + "',NO_TELP='" + no_telp + "',EMAIL_BIRO='" + email_biro + "' WHERE ID_BIRO='" + BiroId + "'";
            s.executeUpdate(sql);
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Data Biro berhasil diupdate");
            rs.close();
            s.close();
            conn.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Data Biro Gagal Diupdate");
            Logger.getLogger(Paket.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    public ArrayList lihatDataBiro() {
        ArrayList biros = new ArrayList();
        try {
            Connection connection = new DbBean_1().getKoneksi();
            Statement s = connection.createStatement();
            String sql = "SELECT * from BIRO order by ID_BIRO";
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                Biro br = new Biro();
                br.id_biro = rs.getString(1);
                br.nama_biro = rs.getString(2);
                br.Alamat = rs.getString(3);
                br.no_telp = rs.getString(4);
                br.email_biro = rs.getString(5);
                biros.add(br);
            }
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException e) {
        }
        return biros;
    }

      public void hapusBiro(String BiroId) {
        try {
            Connection connection = new DbBean_1().getKoneksi();
            Statement s = connection.createStatement();
//            String sql = "insert into PAKET values('" + idPaket + "', '" + namaPaket + "', to_Date('" + waktu + "', 'DD-MM-YY'), " + harga + ",'" + armada + "','" + fasilitas + "','" + tujuan + "'," + kuota + ")";
            String sql = "DELETE FROM BIRO WHERE ID_BIRO='" + BiroId + "',NAMA_BIRO='" + nama_biro + "',ALAMAT='" + Alamat + "',NO_TELP='" + no_telp + "',EMAIL_BIRO='" + email_biro + "'";
            ResultSet rs = s.executeQuery(sql);
            JOptionPane.showMessageDialog(null, "Anda telah berhasil menghapus Biro");
            rs.close();
            s.close();
            connection.close();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Maaf Anda gagal menghapus Biro" + ex);
            Logger.getLogger(Biro.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    
}
